package com.rex.saas.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.rex.saas.bean.DeviceBasicData;
import com.rex.saas.bean.DeviceGroup;
import com.rex.saas.bean.DeviceInfoData;
import com.rex.saas.bean.response.DeviceResponse;
import com.rex.saas.db.entity.SaasDeviceGroup;
import com.rex.saas.db.entity.SaasDeviceGroupRelation;
import com.rex.saas.db.entity.SaasDeviceInfo;
import com.rex.saas.db.entity.SaasSpaceDeviceRelation;
import com.rex.saas.db.mapper.SaasDeviceGroupMapper;
import com.rex.saas.db.mapper.SaasDeviceGroupRelationMapper;
import com.rex.saas.db.mapper.SaasDeviceInfoMapper;
import com.rex.saas.db.mapper.SaasSpaceDeviceRelationMapper;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName GroupService
 * @Description:
 * @Author: zhusiyu
 */
@Component
@Slf4j
public class GroupService {
    @Autowired
    private SaasDeviceInfoMapper saasDeviceInfoMapper;

    @Autowired
    private SaasSpaceDeviceRelationMapper saasSpaceDeviceRelationMapper;

    private SaasDeviceGroupMapper saasDeviceGroupMapper;

    @Autowired
    private SaasDeviceGroupRelationMapper saasDeviceGroupRelationMapper;

    @Autowired
    private RexolarApiClient rexolarApiClient;

    @Autowired
    private DeviceService deviceService;

    public List<DeviceGroup> list(Long spaceId) {
        List<DeviceGroup> result = new ArrayList<>();
        List<SaasDeviceGroup> list = saasDeviceGroupMapper.selectList(new LambdaQueryWrapper<SaasDeviceGroup>().eq(SaasDeviceGroup::getSpaceId, spaceId));
        for (SaasDeviceGroup saasDeviceGroup : list) {
            DeviceGroup deviceGroup = new DeviceGroup();
            deviceGroup.setId(saasDeviceGroup.getId());
            deviceGroup.setGroupName(saasDeviceGroup.getGroupName());
            deviceGroup.setGmtCreated(saasDeviceGroup.getGmtCreated());
            result.add(deviceGroup);
        }
        return result;
    }

    public SaasDeviceGroup getGroupById(Long groupId) {
        return saasDeviceGroupMapper.selectById(groupId);
    }

    public List<DeviceInfoData> queryDevices(Long groupId, String token) {
        List<DeviceInfoData> result = new ArrayList<>();
        SaasDeviceGroup saasDeviceGroup = saasDeviceGroupMapper.selectById(groupId);
        if (saasDeviceGroup == null){
            log.error("queryDevices saasDeviceGroupMapper.selectById error, groupId:{}", groupId);
            return result;
        }
        List<SaasDeviceGroupRelation> list = saasDeviceGroupRelationMapper.selectList(new LambdaQueryWrapper<SaasDeviceGroupRelation>()
                .eq(SaasDeviceGroupRelation::getGroupId, groupId));
        for (SaasDeviceGroupRelation saasDeviceGroupRelation : list) {
            List<SaasDeviceInfo> devices = saasDeviceInfoMapper.selectList(new LambdaQueryWrapper<SaasDeviceInfo>().eq(SaasDeviceInfo::getDeviceName, saasDeviceGroupRelation.getDeviceName()));
            if (CollectionUtils.isEmpty(devices)|| devices.size()>1){
                log.error("queryDevices saasDeviceInfoMapper.selectList error, saasDeviceGroupRelation.deviceName:{}", saasDeviceGroupRelation.getDeviceName());
                continue;
            }
            DeviceInfoData deviceInfoData = new DeviceInfoData();
            SaasDeviceInfo saasDeviceInfo = devices.get(0);
            BeanUtils.copyProperties(saasDeviceInfo, deviceInfoData);
            List<SaasSpaceDeviceRelation> spaceDeviceRelations = saasSpaceDeviceRelationMapper.selectList(new LambdaQueryWrapper<SaasSpaceDeviceRelation>()
                    .eq(SaasSpaceDeviceRelation::getDeviceName, saasDeviceInfo.getDeviceName())
                    .eq(SaasSpaceDeviceRelation::getSpaceInfoId, saasDeviceGroup.getSpaceId()));
            if (!CollectionUtils.isEmpty(spaceDeviceRelations)){
                SaasSpaceDeviceRelation spaceDeviceRelation = spaceDeviceRelations.get(0);
                deviceInfoData.setPosition(spaceDeviceRelation.getPosition());
            }
            String tenantToken = rexolarApiClient.getTenantToken(saasDeviceInfo.getTenantId());
            DeviceResponse deviceResponse = deviceService.getDeviceResponse(saasDeviceInfo, tenantToken);
            deviceInfoData.setLastUpdateTime(deviceResponse.getLastUpdateTime());
            deviceInfoData.setStatus(deviceResponse.getStatus());
            result.add(deviceInfoData);
        }
        return result;
    }

    public boolean addDevice(Long groupId, String deviceName) {
        SaasDeviceGroupRelation saasDeviceGroupRelation = new SaasDeviceGroupRelation();
        saasDeviceGroupRelation.setGroupId(groupId);
        saasDeviceGroupRelation.setDeviceName(deviceName);
        return saasDeviceGroupRelationMapper.insert(saasDeviceGroupRelation) == 1;
    }

    public SaasDeviceGroupRelation getDeviceRelationById(Long id) {
        return saasDeviceGroupRelationMapper.selectById(id);
    }

    public boolean deleteDevice(Long id) {
        return saasDeviceGroupRelationMapper.deleteById(id) == 1;
    }

    public List<SaasDeviceGroupRelation> getDevices(Long groupId) {
        return saasDeviceGroupRelationMapper.selectList(new LambdaQueryWrapper<SaasDeviceGroupRelation>().eq(SaasDeviceGroupRelation::getGroupId, groupId));
    }
}
